#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int a[maxn];
int n, k;
int main() {
	cin >> n >> k;
	for (int i=1; i<=n; i++) {
		cin >> a[i];
	}
	sort(a+1, a+n+1);
	int sum_max = 0;
	for (int i=n; i>=1; i--) {
		int j=i;
		int max = a[i]; 
		int tmp_max = 0;
		while (j>=1 && (max - a[j]) <= k) {
			tmp_max += a[j];
			j--; 
		}
		if (sum_max < tmp_max) {
			sum_max = tmp_max;
		}
	}
	cout << sum_max;
} 
